<template>
	<view class="page">
		<u-navbar
			:autoBack="true"
			placeholder
		>
			<view class="" slot="right">
				<view class="btn">
					<u-button type="primary" :customStyle="{height: '60rpx'}" color="#2979ff" text="确定" @click="confirm"></u-button>
				</view>
			</view>
		</u-navbar>
		<view class="page__my" v-show="menuValue == 1">
			<view class="list" style="padding-left: 30rpx;box-sizing: border-box;" v-for="(item,index) in selectList" :key="item.Id">
				<view class="list__cover" @click="openForm(item)">
					<image :src="item.cover" mode="aspectFill"></image>
				</view>
				<view class="list__content" @click="openForm(item)">
					<view class="list__content--title ellipsis">{{item.FormTitle}}</view>
					<view class="list__content--des ellipsis">{{item.FormDesc}}</view>
				</view>
				<view class="list__del center" @click="del(item,index)">
					<image src="@/static/theme/default/home/henggang.png" mode="aspectFill"></image>
				</view>
			</view>
			
			
		</view>
		<view class="page__all" v-show="menuValue == 0">
			<view class="page__head">
				<view class="page__head--right">
					<view class="page__head--left__icon center">
						<u-icon name="search" size="25" @click="goSeach"></u-icon>
					</view>
				</view>
				<view class="page__head--left">
					<view class="page__head--left__icon center">
						<c-sort @change="changeMode"></c-sort>
					</view>
				</view>
			</view>
			<view class="page__category">
				<view class="page__category--list">
					<view class="page__category--list-item" :class="category_index == index ? 'category-active' : ''" v-for="(item, index) in categories" :key="index" @click="changeCategory(index)">{{item.TypeName}}</view>
				</view>
				<u-popup mode="top" :overlay="false" duration="0" :customStyle="{marginTop: '250rpx'}" :show="categoryVisible" @close="categoryVisible = false" @open="categoryVisible = true">
					<view class="category-pop">
						<view class="category-pop__head">
							<view class="category-pop__head--title">全部分类</view>
							<view class="category-pop__head--icon">
								<u-icon name="close" size="20" @click="categoryVisible = false"></u-icon>
							</view>
						</view>
						<view class="category-pop__content">
							<view class="category-pop__content--list" :class="category_index == index ? 'category-active' : ''" v-for="(item, index) in categories" :key="index" @click="changeCategory(index)">{{item.TypeName}}</view>
							<view class="category-pop__content--list" style="background-color: #fff;" v-for="item in (3 - categories.length % 3)" v-show="categories.length % 3 != 0"></view>
						</view>
					</view>
				</u-popup>
				<view class="page__category--icon" @click="openAllCategory">
					<u-icon name="grid" size="25"></u-icon>
				</view>
			</view>
			<view class="content">
				<view class="list" v-for="(item,index) in list" :key="item.Id">
					<view class="list__icon center" @click="choose(item)">
						<view class="list__icon--box">
							<image src="@/static/theme/default/home/checked.png" mode="" v-if="item.checked"></image>
						</view>
					</view>
					<view class="list__cover" @click="openForm(item)">
						<image :src="item.cover" mode="aspectFill"></image>
					</view>
					<view class="list__content" @click="openForm(item)">
						<view class="list__content--title ellipsis">{{item.FormTitle}}</view>
						<view class="list__content--des ellipsis">{{item.FormDesc}}</view>
					</view>
				</view>
			</view>
			<view class="no-more" v-if="noMore">没有更多了</view>
			<no-data v-if="noData"></no-data>
		</view>
		
		<u-tabbar
			:value="menuValue"
			@change="changeMenu"
		>
			<u-tabbar-item text="表单" icon="list"></u-tabbar-item>
			<u-tabbar-item text="已选择" icon="checkmark" :badge="selectList.length"></u-tabbar-item>
		</u-tabbar>
	</view>
</template>

<script>
	import _custom from '@/common/_custom.js';
	import _data from '@/common/_data.js';
	import cSort from '@/components/custom/c-sort/c-sort.vue'
	import NoData from '@/components/custom/no-data/no-data.vue'
	import {sendBackFunction} from '@/common/custom_util.js'
	
	export default {
		components: {
			cSort,
			NoData
		},
		data() {
			return {
				static_img: _data.data('static_img'),
				pxid: _data.localData('userid'),
				list: [],
				categories: [],
				category: '',
				category_index: 0,
				categoryVisible: false,
				mode: 'asc',
				currentpage: 1,
				pagesize: 10,
				noMore: false,
				noData: false,
				menuValue: 0,
				selectList: [],
				isInit: false,
				formids: "",
			}
		},
		onLoad(options) {
			this.formids = options.formids;
			this.getSmartFormListInBusiness().then(() => {
				this.init();
				this.getSmartFormType();
			});
		},
		onNavigationBarButtonTap() {
			uni.navigateTo({
				url: '/pages/home/child/smart-form-add/index'
			})
		},
		onReachBottom() {
			if(!this.noMore) {
				this.currentpage += 1;
				this.getSmartFormList();
			}
		},
		methods: {
			init() {
				this.isInit = true;
				this.list = [];
				this.getSmartFormList();
			},
			/**
			 * 获取表单模板类型
			 * @param {Object} v
			 */
			getSmartFormType(v) {
				let that = this;
				_custom.getDataJava({
					apiname: 'getSmartFormType',
					success(res) {
						res.unshift({
							TypeName: '全部'
						})
						that.categories = res;
					},
				})
			},
			/**
			 * 获取表单模板列表
			 * @param {Object} v
			 */
			getSmartFormList() {
				let that = this;
				_custom.getDataJava({
					apiname: 'getSmartFormList',
					pagesize: this.pagesize,
					currentpage: this.currentpage,
					apiparam: {
						keyword: "", //关键词模糊查询
						typename: this.category,
						sorting: this.mode, //asc:最早，desc:最新
					},
					success(res) {
						if(res.length < that.pagesize) that.noMore = true;
						if(that.currentpage == 1 && !res.length) {
							that.noMore = false;
							that.noData = true;
							that.list = [];
							return
						}
						that.noData = false;
						res.forEach(item => {
							item.cover = that.static_img + '/' + item.CoverImage;
							try {
								let json = item.FormJson.replace(/%22/g,'"');
								item.FormJson = JSON.parse(json);
							} catch(e){console.log(e)}
						})
						that.list.push(...res);
						if(that.selectList.length) {
							that.list.forEach(v => {
								that.selectList.forEach(item => {
									if(item.Id == v.Id) {
										v.checked = true;
									}
								})
							})
						}
					},
				})
			},
			/**
			 * 获取业务关联的表单填写情况列表
			 */
			getSmartFormListInBusiness() {
				let that = this;
				return new Promise(resolve => {
					_custom.getDataJava({
						apiname: 'getSmartFormListInBusiness', 
						apiparam: {
							formids: this.formids,
							businesscode: 'Subject',
							businessid: "",
						},
						success(res) {
							res.forEach(item => {
								item.LastTime ? item.lastTime = timestampFormat(item.LastTime) : '';
								item.cover = that.static_img + '/' + item.CoverImage;
								try {
									let json = item.FormJson.replace(/%22/g,'"');
									item.FormJson = JSON.parse(json);
								} catch(e){console.log(e)}
								item.Id = item.FormId
							})
							that.selectList = res;
							resolve()
						}
					})
				})
			},
			openAllCategory() {
				this.categoryVisible = true;
			},
			changeCategory(i) {
				this.category_index = i;
				let value = this.categories[i].TypeName;
				value = value == '全部' ? '' : value;
				this.category = value;
				this.init();
				this.categoryVisible = false;
			},
			goDraft() {
				uni.navigateTo({
					url: './draft'
				})
			},
			openForm(v) {
				let obj = JSON.parse(JSON.stringify(v));
				uni.navigateTo({
					url: '../smart-form-add/view?type=1&data=' + JSON.stringify(obj)
				})
			},
			goSeach() {
				uni.navigateTo({
					url: './select-search?data=' + JSON.stringify(this.selectList),
					animationType: 'none'
				})
			},
			changeMode(e) {
				this.mode = e;
				this.init();
			},
			changeMenu(v) {
				this.menuValue = v;
			},
			choose(v) {
				this.$set(v,'checked',!v.checked);
				let flag = false;
				let i = -1;
				this.selectList.forEach((item,index) => {
					if(item.Id == v.Id) {
						flag = true;
						i = index;
					}
				})
				if(!flag) {
					this.selectList.push(JSON.parse(JSON.stringify(v)))
				} else {
					this.selectList.splice(i,1);
				}
			},
			del(v,i) {
				this.selectList.splice(i,1);
				this.list.forEach(item => {
					if(item.Id == v.Id) {
						item.checked = false;
					}
				})
			},
			searchSelect(v) {
				this.selectList = v;
				this.list.forEach(item => {
					this.$set(item,'checked',false);
					this.selectList.forEach(value => {
						if(item.Id == value.Id) this.$set(item,'checked',true);
					})
				})
			},
			confirm() {
				let formids = this.selectList.map(item => {
					return item.Id;
				})
				sendBackFunction('getForm',formids)
			}
		}
	}
</script>

<style scoped lang="scss">
	image {
		width: 100%;
		height: 100%;
	}
	
	.page {
		
		&__head {
			position: fixed;
			left: 0;
			right: 0;
			top: calc(var(--status-bar-height) + 44px);
			z-index: 9;
			height: 100rpx;
			padding: 0 20rpx;
			background-color: #fff;
			display: flex;
			justify-content: space-between;
			align-items: center;
			
			&--left {
				display: flex;
				
				&__icon {
					
				}
			}
			
			&--right {
				
				&-box {
					padding: 10rpx;
					border-radius: 10rpx;
					background-color: #bae0ff;
					color: #1677ff;
					display: flex;
					align-items: center;
				}
			}
		}
		
		&__category {
			position: fixed;
			left: 0;
			right: 0;
			top: calc(var(--status-bar-height) + 44px + 100rpx);
			z-index: 9;
			height: 100rpx;
			padding-left: 20rpx;
			box-sizing: border-box;
			background-color: #fff;
			display: flex;
			justify-content: space-between;
			align-items: center;
			
			&--list {
				width: 650rpx;
				display: flex;
				overflow-x: scroll;
				flex-wrap: nowrap;
				align-items: center;
				
				&-item {
					padding: 0 20rpx;
					font-size: 33rpx;
					flex-shrink: 0;
				}
			}
			
			&--icon {
				width: 100rpx;
				display: flex;
				justify-content: center;
				align-items: center;
			}
		}
	}
	
	.content {
		margin-top: 200rpx;
	}
	
	.list {
		height: 200rpx;
		border-radius: 20rpx;
		flex-shrink: 0;
		background-color: #fff;
		display: flex;
		align-items: center;
		
		&__icon {
			width: 120rpx;
			height: 100%;
			
			&--box {
				width: 40rpx;
				height: 40rpx;
				border-radius: 50%;
				border: 3rpx solid #d9d9d9;
			}
		}
		
		&__cover {
			width: 200rpx;
			height: 120rpx;
		}
		
		&__content {
			width: calc(100% - 320rpx);
			padding: 0 20rpx;
			box-sizing: border-box;
			
			&--title {
				font-size: 35rpx;
			}
			
			&--des {
				font-size: 26rpx;
				color: #aaa;
			}
		}
		
		&__del {
			width: 120rpx;
			height: 100%;
			
			image {
				width: 40rpx;
				height: 40rpx;
			}
		}
	}
	
	.category-pop {
		background-color: #fff;
		
		&__head {
			height: 100rpx;
			margin: 0 20rpx;
			box-sizing: border-box;
			position: relative;
			border-bottom: 1rpx solid #eee;
			
			&--title {
				text-align: center;
				line-height: 100rpx;
			}
			
			&--icon {
				position: absolute;
				right: 20rpx;
				top: 30rpx;
			}
		}
		
		&__content {
			display: flex;
			justify-content: space-around;
			flex-wrap: wrap;
			padding: 20rpx;
			
			&--list {
				width: 200rpx;
				height: 60rpx;
				font-size: 33rpx;
				text-align: center;
				line-height: 60rpx;
				border-radius: 20rpx;
				background-color: #f0f0f0;
				margin-top: 20rpx;
			}
		}
	}
	
	.category-active {
		color: #91caff;
	}
	
	.no-more {
		height: 100rpx;
		text-align: center;
		line-height: 100rpx;
	}
	
	.btn {
		width: 110rpx;
	}
</style>
